package org.ss.psci.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.ss.psci.entity.PmAssignment;
import org.ss.psci.entity.PmContracts;
import org.ss.psci.util.DateUtil;

/**
 * Data access object (DAO) for domain model class PmContracts.
 * 
 * @see org.ss.psci.entity.PmContracts
 * @author MyEclipse Persistence Tools
 */

public class PmContractsDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(PmContractsDAO.class);
	// property constants
	public static final String YI_FANG = "yiFang";
	public static final String JIA_FANG = "jiaFang";
	public static final String NUMBER = "Number";
	public static final String START_TIME = "startTime";
	public static final String END_TIME = "endTime";
	public static final String PRICE = "price";
	public static final String REMARKS = "remarks";
	public static final String SIGN_DATE = "signDate";
	public static final String MANAGE_DEP = "manageDep";
	public static final String FILEPATH = "filepath";
	public static final String CONS_TYPE = "consType";
	public static final String PMPROJECTS = "pmProjects";

	public static final String COMPANYID = "companyId";
	public static final String PROJECTID = "projectId";

	public static final String CONSID = "consId";
	public static final String CONSNAME = "consName";

	public List findByCompanyID(Object name) {
		return findByProperty(COMPANYID, name);
	}

	public List findByProjectId(Object name) {
		return findByProperty(PROJECTID, name);
	}

	public List findByConsId(Object name) {
		return findByProperty(CONSID, name);
	}

	public List findByConsName(Object name) {
		return findByProperty(CONSNAME, name);
	}

	private String getConSeq() {
		String seq = "";
		String date = new DateUtil().getDateString().replaceAll("-", "")
				.replaceAll(":", "");
		String time = new DateUtil().getTimeString().replaceAll("-", "")
				.replaceAll(":", "");
		System.out.println("HT" + date + time);
		return "HT" + date + time;
	}

	public PmContracts copyEntity(PmContracts dest, PmContracts orig) {
		if (dest == null || orig == null) {
			return new PmContracts();
		}
		if (orig.getJiaFang() != null) {
			dest.setJiaFang(orig.getJiaFang());
		}
		if (orig.getYiFang() != null) {
			dest.setYiFang(orig.getYiFang());
		}
		if (orig.getPrice() != null) {
			dest.setPrice(orig.getPrice());
		}
		if (orig.getSignDate() != null) {
			dest.setSignDate(orig.getSignDate());
		}
		if (orig.getStartTime() != null) {
			dest.setStartTime(orig.getStartTime());
		}
		if (orig.getManageDep() != null) {
			dest.setManageDep(orig.getManageDep());
		}
		if (orig.getEndTime() != null) {
			dest.setEndTime(orig.getEndTime());
		}
		if (orig.getFilepath() != null) {
			dest.setFilepath(orig.getFilepath());
		}
		if (orig.getConsType() != null) {
			dest.setConsType(orig.getConsType());
		}
		if (orig.getJiaFangBill() != null) {
			dest.setJiaFangBill(orig.getJiaFangBill());
		}
		if (orig.getJiaFangMsgr() != null) {
			dest.setJiaFangMsgr(orig.getJiaFangMsgr());
		}
		if (orig.getYiFangMsgr() != null) {
			dest.setYiFangMsgr(orig.getYiFangMsgr());
		}
		if (orig.getYiFangBill() != null) {
			dest.setYiFangBill(orig.getYiFangBill());
		}
		if (orig.getStatus() != null) {
			dest.setStatus(orig.getStatus());
		}
		if (orig.getAddress() != null) {
			dest.setAddress(orig.getAddress());
		}
		if (orig.getPhone() != null) {
			dest.setPhone(orig.getPhone());
		}
		if (orig.getConsId() != null) {
			dest.setConsId(orig.getConsId());
		}
		if (orig.getProjectId() != null) {
			dest.setProjectId(orig.getProjectId());
		}
		if (orig.getUsername() != null) {
			dest.setUsername(orig.getUsername());
		}
		if (orig.getConsName() != null) {
			dest.setConsName(orig.getConsName());
		}
		if (orig.getJiaFangAddress() != null) {
			dest.setJiaFangAddress(orig.getJiaFangAddress());
		}
		if (orig.getJiaFangPhone() != null) {
			dest.setJiaFangPhone(orig.getJiaFangPhone());
		}
		if (orig.getYiFangAddress() != null) {
			dest.setYiFangAddress(orig.getYiFangAddress());
		}
		if (orig.getYiFangPhone() != null) {
			dest.setYiFangPhone(orig.getYiFangPhone());
		}
		if (orig.getSubmitAddress() != null) {
			dest.setSubmitAddress(orig.getSubmitAddress());
		}
		if (orig.getSubmitType() != null) {
			dest.setSubmitType(orig.getSubmitType());
		}
		if (orig.getArea() != null) {
			dest.setArea(orig.getArea());
		}

		return dest;
	}

	protected void initDao() {
		// do nothing
	}

	public void save(PmContracts transientInstance) {
		log.debug("saving PmContracts instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(PmContracts persistentInstance) {
		log.debug("deleting PmContracts instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public PmContracts findById(java.lang.Integer id) {
		log.debug("getting PmContracts instance with id: " + id);
		try {
			PmContracts instance = (PmContracts) getHibernateTemplate().get(
					"org.ss.psci.entity.PmContracts", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(PmContracts instance) {
		log.debug("finding PmContracts instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding PmContracts instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from PmContracts as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName1, Object value1,
			String propertyName2, Object value2) {
		log.debug("finding PmContracts instance with property: "
				+ propertyName1 + ", value: " + value1);
		try {
			String queryString = "from PmContracts as model where model."
					+ propertyName1 + "= ? and model." + propertyName2 + "=?";
			return getHibernateTemplate().find(queryString,
					new Object[] { value1, value2 });
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByConsName(Object param1, Object param2) {
		return findByProperty(CONSNAME, param1, PROJECTID, param2);
	}

	public List findByConsId(Object param1, Object param2) {
		return findByProperty(CONSID, param1, PROJECTID, param2);
	}

	public List findBySignDate(Object param1, Object param2) {
		return findByProperty(SIGN_DATE, param1, PROJECTID, param2);
	}

	public List findByJiaFang(Object param1, Object param2) {
		return findByProperty(JIA_FANG, param1, PROJECTID, param2);
	}

	public List findByYiFang(Object param1, Object param2) {
		return findByProperty(YI_FANG, param1, PROJECTID, param2);
	}

	public List findByNumber(Object number) {
		return findByProperty(NUMBER, number);
	}

	public List findByJiaFang(Object jiaFang) {
		return findByProperty(JIA_FANG, jiaFang);
	}

	public List findByPmProject(Object pmProjects) {
		return findByProperty(PMPROJECTS, pmProjects);
	}

	public List findByYiFang(Object yiFang) {
		return findByProperty(YI_FANG, yiFang);
	}

	public List findByStartTime(Object startTime) {
		return findByProperty(START_TIME, startTime);
	}

	public List findByEndTime(Object endTime) {
		return findByProperty(END_TIME, endTime);
	}

	public List findByPrice(Object price) {
		return findByProperty(PRICE, price);
	}

	public List findByRemarks(Object remarks) {
		return findByProperty(REMARKS, remarks);
	}

	public List findBySignDate(Object signDate) {
		return findByProperty(SIGN_DATE, signDate);
	}

	public List findByManageDep(Object manageDep) {
		return findByProperty(MANAGE_DEP, manageDep);
	}

	public List findByFilepath(Object filepath) {
		return findByProperty(FILEPATH, filepath);
	}

	public List findByConsType(Object consType) {
		return findByProperty(CONS_TYPE, consType);
	}

	public List findAll() {
		log.debug("finding all PmContracts instances");
		try {
			String queryString = "from PmContracts";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public PmContracts merge(PmContracts detachedInstance) {
		log.debug("merging PmContracts instance");
		try {
			PmContracts result = (PmContracts) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(PmContracts instance) {
		log.debug("attaching dirty PmContracts instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(PmContracts instance) {
		log.debug("attaching clean PmContracts instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static PmContractsDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (PmContractsDAO) ctx.getBean("PmContractsDAO");
	}
}